package com.wocao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.*;
import com.wocao.entity.ZyRoom;
import com.wocao.entity.ZyRoom;
import com.wocao.service.ZyRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 房间  前端控制器
 * </p>
 *
 * @author gyh
 * @since 2023-12-03
 */
@RestController
@RequestMapping("/zyRoom")
public class ZyRoomController {
    @Autowired
    private ZyRoomService zyRoomService;

    private SysDictDataController sysDictDataController;

    @GetMapping("rooms")
    public ResponseResult<Page<ZyRoom>> findJointPage(@RequestParam(required = false, defaultValue = "1") Long current,
                                                      @RequestParam(required = false, defaultValue = "2") Long size,
                                                      @RequestParam(required = false, defaultValue = "") String buildingId,
                                                      @RequestParam(required = false, defaultValue = "") String unitId,
                                                      @RequestParam(required = false, defaultValue = "") String roomStatus,
                                                      @RequestParam(required = false, defaultValue = "") String communityId) {
        try {
            Page<ZyRoom> page = zyRoomService.getRoomsByLike(new Page<>(current, size), buildingId, unitId, roomStatus, communityId);
            List<ZyRoom> records = page.getRecords();
            for (ZyRoom record : records) {
                LocalDateTime createTime = record.getCreateTime();
                record.getRoomStatus();
                String formattedCreateTime = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                record.setFormattedCreateTime(formattedCreateTime);
            }
            return ResponseResult.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "查询失败:" + e.getMessage());
        }
    }

    @PostMapping("room/{username}")
    public ResponseResult<String> addRoom(@RequestBody ZyRoom room, @PathVariable String username) {
        try {
            Random random = new Random();
            String prefix = "ROOM_160";
            StringBuilder sb = new StringBuilder(prefix);

            for (int i = 0; i < 10; i++) {
                int digit = random.nextInt(10);
                sb.append(digit);
            }

            room.setRoomCode(sb.toString());
            room.setCreateBy(username);
            room.setCreateTime(LocalDateTime.now());
            zyRoomService.save(room);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "添加失败:" + e.getMessage());
        }
    }

    @PutMapping("room/{username}")
    public ResponseResult<String> updateRoom(@RequestBody ZyRoom room, @PathVariable String username) {
        try {
            room.setUpdateBy(username);
            room.setUpdateTime(LocalDateTime.now());
            zyRoomService.updateById(room);
            return ResponseResult.ok(room);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "修改失败:" + e.getMessage());
        }
    }

    @DeleteMapping("room/{roomIds}")
    public ResponseResult<String> deleteRoom(@PathVariable List<String> roomIds) {

        try {
            zyRoomService.removeByIds(roomIds);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "删除失败:" + e.getMessage());
        }
    }

    @GetMapping("export")
    public void exportExcel(HttpServletResponse response, @RequestParam("roomIds") String[] roomIds) {
        try {
            zyRoomService.export(response, roomIds);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("buildingsAndUnits")
    public ResponseResult<Building> findBuildingAndUnit() {

        try {
            List<Building> buildings = zyRoomService.getBuildingAndUnit();
            return ResponseResult.ok(buildings);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "查询失败:" + e.getMessage());
        }
    }

    @GetMapping("rooms/{unitId}")
    public ResponseResult<ZyRoom> findRoomsByBuildingId(@PathVariable String unitId) {
        List<ZyRoom> roomList = zyRoomService.getRoomsByBuildingId(unitId);
        return ResponseResult.ok(roomList);
    }

}
